package org.example;

import org.example.model.Command.Command;
import org.example.model.Command.StartTaskCommand;
import org.example.model.Factory.FarmMachineFactory;
import org.example.model.Factory.HarvesterFactory;
import org.example.model.Factory.SeederFactory;
import org.example.model.Observable.EnvironmentSensor;
import org.example.model.PathStrategy.OptimizedPathStrategy;
import org.example.model.SafetyHandler.BoundaryHandler;
import org.example.model.SafetyHandler.FaultHandler;
import org.example.model.SafetyHandler.ObstacleHandler;
import org.example.model.SafetyHandler.SafetyHandler;
import org.example.other.EventType;
import org.example.other.FieldData;
import org.example.other.SafetyEvent;
import org.example.other.SensorData;
import org.example.system.FarmMachine;

public class SmartFarmSystem {
    public static void main(String[] args) {
        System.out.println("=============== 智能无人驾驶农机系统启动 ===============");

        // 1. 使用工厂方法创建农机设备
        FarmMachineFactory harvesterFactory = new HarvesterFactory();
        FarmMachine harvester = harvesterFactory.createMachine("H-001");

        FarmMachineFactory seederFactory = new SeederFactory();
        FarmMachine seeder = seederFactory.createMachine("S-001");

        // 2. 初始化环境传感器
        EnvironmentSensor moistureSensor = new EnvironmentSensor("土壤湿度");
        EnvironmentSensor phSensor = new EnvironmentSensor("土壤PH值");

        // 3. 注册观察者
        moistureSensor.addObserver(harvester);
        moistureSensor.addObserver(seeder);
        phSensor.addObserver(harvester);

        // 4. 创建安全处理责任链
        SafetyHandler obstacleHandler = new ObstacleHandler();
        SafetyHandler faultHandler = new FaultHandler();
        SafetyHandler boundaryHandler = new BoundaryHandler();

        obstacleHandler.setNext(faultHandler);
        faultHandler.setNext(boundaryHandler);

//        // 5. 初始化定位系统
//        NavigationSystem navigation = NavigationSystem.getInstance();
//
//        // 6. 设置设备位置
//        harvester.position = navigation.getPosition();
//        seeder.position = navigation.getPosition();

        // 7. 模拟传感器数据变化
        moistureSensor.setData(new SensorData("土壤湿度", 85.0));
        phSensor.setData(new SensorData("土壤PH值", 6.8));

        // 8. 路径规划演示
        FieldData fieldA = new FieldData("A-1");
        fieldA.addReading("障碍物", 1);
        harvester.planPath(fieldA);

        // 9. 命令模式演示
        Command startHarvest = new StartTaskCommand(harvester, "北区小麦收割");
        startHarvest.execute();

        Command startSeeding = new StartTaskCommand(seeder, "南区玉米播种");
        startSeeding.execute();

        // 10. 状态模式演示
        harvester.displayStatus();
        seeder.displayStatus();

        // 11. 安全事件处理演示
        System.out.println("\n=== 安全系统测试 ===");
        obstacleHandler.handle(new SafetyEvent(EventType.OBSTACLE));
        obstacleHandler.handle(new SafetyEvent(EventType.MACHINE_FAULT));

//        // 12. 适配器模式演示
//        System.out.println("\n=== 集成新设备 ===");
//        NewWeeder newWeeder = new NewWeeder();
//        FarmMachine weederAdapter = new NewWeederAdapter("W-001", newWeeder);
//        Command startWeeding = new StartTaskCommand(weederAdapter, "东区除草作业");
//        startWeeding.execute();

        // 13. 多机协同路径优化
        System.out.println("\n=== 多机协同路径优化 ===");
        harvester.setPathStrategy(new OptimizedPathStrategy());
        harvester.planPath(fieldA);

        System.out.println("\n=============== 系统运行结束 ===============");
    }
}